/*
 * @Author: 王鹏 2911783749@qq.com
 * @Date: 2022-11-07 09:06:03
 * @LastEditors: 王鹏 2911783749@qq.com
 * @LastEditTime: 2022-11-08 16:38:37
 * @FilePath: \vue3Mixiu\code\src\router\index.ts
 * @Description: 路由配置表
 */
// createRouter 创建路由模块
// createWebHistory 创建 路由模式
// RouteRecordRaw 增加路由对象类型限制 好处：允许在基础路由里面增加开发者自定义属性
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'Home',
        component: () => import('../views/Home.vue'),
        children:[
            {
                path:'/',
                component:()=>import("../views/Index.vue")
            },
            {
                path:'/profiles',
                component:()=>import("../views/Profiles.vue")
            },
            {
                path:"/userInfo",
                component:()=>import("../views/UserInfo.vue")
            }
        ]
    },
    {
        path: '/register',
        name: 'register',
        component: () => import('../views/Register.vue')
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('../views/Login.vue')
    },
    {
        path: '/:catchAll(.*)',  //自定义路由类型catchAll (.*)匹配规则
        name: '404',
        component: () => import('../views/404.vue')
    },
]
const router = createRouter({
    history: createWebHistory(),  //创建路由模式为history
    routes: routes //引入路由表
})

// 全局前置路由守卫(页面鉴权)
router.beforeEach((to, from, next) => {
    const isLogin: boolean = sessionStorage.getItem('token') ? true : false
    if (to.path === "/login" || to.path === "/register") {
        next()
    } else {
        isLogin ? next() : next('/login')
    }
})
export default router